﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using VDataAdapter.BlockingQueue;


namespace VDataAdapter.Communication
{
    public enum MessageFlagSequeue
    {
        NULL,
        BEGIN,
        END,
        END_RECOVERY
    };

    /// <summary>
    /// Helper class to:
    /// - Map Vdata Flag to String
    /// - Convert back & forth
    /// 
    /// </summary>
    static class VDataFlag
    {
        public static string GetFlag(MessageFlagSequeue flag)
        {
            if (flag == MessageFlagSequeue.BEGIN)
                return "BEGIN#_#_";
            else
                if (flag == MessageFlagSequeue.END)
                    return "END#_#_";
                else
                    if(flag == MessageFlagSequeue.END_RECOVERY)
                        return "END_RECOVERY#_#_";

            throw new ArgumentException("Unknown string associated with this message flag: " + flag.ToString());
        }

        public static MessageFlagSequeue GetFlag(string msg)
        {
            if (isFlag(msg, MessageFlagSequeue.BEGIN)) return MessageFlagSequeue.BEGIN;
            if (isFlag(msg, MessageFlagSequeue.END)) return MessageFlagSequeue.END;
            if (isFlag(msg, MessageFlagSequeue.END_RECOVERY)) return MessageFlagSequeue.END_RECOVERY;
            
            if(!isValidFlag(msg))
                throw new InvalidOperationException("Unknown flag is in used");
            return MessageFlagSequeue.NULL;
        }

        private static bool isFlag(string msg, MessageFlagSequeue flag)
        {
            if (GetFlag(flag).Equals(msg, StringComparison.Ordinal))
                return true;
            else
                return false;
        }

        private static bool isValidFlag(string flag)
        {
            if (isFlag(flag, MessageFlagSequeue.BEGIN) ||
                isFlag(flag, MessageFlagSequeue.END) ||
                isFlag(flag, MessageFlagSequeue.END_RECOVERY))
                return true;
            return false;
        }
    }

}
